package goqueryimport// Filter reduces the set of matched elements to those that match the selector string.// It returns a new Selection object for this subset of matching elements.func ( *Selection) ( string) *Selection {return .FilterMatcher(compileMatcher())}// FilterMatcher reduces the set of matched elements to those that match// the given matcher. It returns a new Selection object for this subset// of matching elements.func ( *Selection) ( Matcher) *Selection {returnpushStack(, winnow(.Nodes, , true))}// Not removes elements from the Selection that match the selector string.// It returns a new Selection object with the matching elements removed.func ( *Selection) ( string) *Selection {return .NotMatcher(compileMatcher())}// NotMatcher removes elements from the Selection that match the given matcher.// It returns a new Selection object with the matching elements removed.func ( *Selection) ( Matcher) *Selection {returnpushStack(, winnow(.Nodes, , false))}// FilterFunction reduces the set of matched elements to those that pass the function's test.// It returns a new Selection object for this subset of elements.func ( *Selection) ( func(int, *Selection) bool) *Selection {returnpushStack(, winnowFunction(, , true))}// NotFunction removes elements from the Selection that pass the function's test.// It returns a new Selection object with the matching elements removed.func ( *Selection) ( func(int, *Selection) bool) *Selection {returnpushStack(, winnowFunction(, , false))}// FilterNodes reduces the set of matched elements to those that match the specified nodes.// It returns a new Selection object for this subset of elements.func ( *Selection) ( ...*html.Node) *Selection {returnpushStack(, winnowNodes(, , true))}// NotNodes removes elements from the Selection that match the specified nodes.// It returns a new Selection object with the matching elements removed.func ( *Selection) ( ...*html.Node) *Selection {returnpushStack(, winnowNodes(, , false))}// FilterSelection reduces the set of matched elements to those that match a// node in the specified Selection object.// It returns a new Selection object for this subset of elements.func ( *Selection) ( *Selection) *Selection {if == nil {returnpushStack(, winnowNodes(, nil, true)) }returnpushStack(, winnowNodes(, .Nodes, true))}// NotSelection removes elements from the Selection that match a node in the specified// Selection object. It returns a new Selection object with the matching elements removed.func ( *Selection) ( *Selection) *Selection {if == nil {returnpushStack(, winnowNodes(, nil, false)) }returnpushStack(, winnowNodes(, .Nodes, false))}// Intersection is an alias for FilterSelection.func ( *Selection) ( *Selection) *Selection {return .FilterSelection()}// Has reduces the set of matched elements to those that have a descendant// that matches the selector.// It returns a new Selection object with the matching elements.func ( *Selection) ( string) *Selection {return .HasMatcher(compileMatcher())}// HasMatcher reduces the set of matched elements to those that have a descendant// that matches the matcher.// It returns a new Selection object with the matching elements.func ( *Selection) ( Matcher) *Selection { := make([]*html.Node, 0, len(.Nodes))// Manually create a singleMatcher (rather than using SingleMatcher, which // returns a Matcher interface) so we can call its MatchFirst method // directly. MatchFirst probes for the first match in a subtree without // building a result slice, avoiding a throwaway one-element slice // allocation for every matching child subtree. := singleMatcher{}for , := range .Nodes {for := .FirstChild; != nil; = .NextSibling {if .Type == html.ElementNode && .MatchFirst() != nil { = append(, )break } } }returnpushStack(, )}// HasNodes reduces the set of matched elements to those that have a// descendant that matches one of the nodes.// It returns a new Selection object with the matching elements.func ( *Selection) ( ...*html.Node) *Selection {var []*html.Nodefor , := range .Nodes {for , := range {ifnodeContains(, ) { = append(, )break } } }returnpushStack(, )}// HasSelection reduces the set of matched elements to those that have a// descendant that matches one of the nodes of the specified Selection object.// It returns a new Selection object with the matching elements.func ( *Selection) ( *Selection) *Selection {if == nil {return .HasNodes() }return .HasNodes(.Nodes...)}// End ends the most recent filtering operation in the current chain and// returns the set of matched elements to its previous state.func ( *Selection) () *Selection {if .prevSel != nil {return .prevSel }returnnewEmptySelection(.document)}// Filter based on the matcher, and the indicator to keep (Filter) or// to get rid of (Not) the matching elements.func winnow( []*html.Node, Matcher, bool) []*html.Node {// Optimize if keep is requestedif {return .Filter() }// Not path: call Match directly on each node, no Selection wrapper needed := make([]*html.Node, 0, len())for , := range {if !.Match() { = append(, ) } }return}// Filter based on an array of nodes, and the indicator to keep (Filter) or// to get rid of (Not) the matching elements.func winnowNodes( *Selection, []*html.Node, bool) []*html.Node {iflen()+len(.Nodes) < minNodesForSet {var []*html.Nodefor , := range .Nodes {ifisInSlice(, ) == { = append(, ) } }return } := make(map[*html.Node]bool, len())for , := range { [] = true }var []*html.Nodefor , := range .Nodes {if [] == { = append(, ) } }return}// Filter based on a function test, and the indicator to keep (Filter) or// to get rid of (Not) the matching elements.func winnowFunction( *Selection, func(int, *Selection) bool, bool) []*html.Node {returngrep(, func( int, *Selection) bool {return (, ) == })}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.